package com.dongdongshop.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPObject;
import com.dongdongshop.mapper.TbContentMapper;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.service.ContentService;
import com.dongdongshop.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@org.springframework.stereotype.Service
public class ContentServiceImpl implements ContentService {
    @Resource
    private TbContentMapper tbContentMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Transactional
    public PageBean<TbContent> getinfo(Integer number, Integer pageSize) {
        PageBean<TbContent> b = new PageBean<>();
        b.setPageNum(number);
        b.setPageSize(pageSize);
        List<TbContent> list = tbContentMapper.getinfo(b);
        List<TbContent> slist= tbContentMapper.getCount();
        b.setResult(list);
        b.setTotal(slist.size());
        return b;
    }

    @Override
    @Transactional
    public Integer toAdd(TbContent tbContent) {
        return tbContentMapper.insertSelective(tbContent);
    }

    @Override
    @Transactional
    public Integer deleteAll(Long[] ids) {
        return tbContentMapper.deleteAll(ids);
    }

    @Override
    @Transactional
    public TbContent toUpdate(Long id) {
        return tbContentMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public Integer updateinfo(TbContent tbContent) {
        return tbContentMapper.updateByPrimaryKeySelective(tbContent);
    }

    //获取轮播图,等于1
    //加缓存
    @Override
    @Transactional
    public List<TbContent> getImage() {
        String key = "CONTENT1";
        List<TbContent> list = null;
        if (redisTemplate.hasKey(key)){
            String str = (String) redisTemplate.opsForValue().get(key);
            list = (List<TbContent>)JSONObject.parse(str);
        }else{
            list = tbContentMapper.getImage();
            String str = JSONObject.toJSONString(list);
            redisTemplate.opsForValue().set(key,str);
        }
        return list;
    }

    //获取今日推荐,等于2
    //缓存
    @Override
    @Transactional
    public List<TbContent> getToday() {
        return tbContentMapper.getToday();
    }

    //获取猜你喜欢类型的,等于4
    //缓存
    @Override
    @Transactional
    public List<TbContent> getLike() {
        return tbContentMapper.getLike();
    }
}
